package com.acme.domain.account;

import com.acme.exceptions.NotEnoughFundsException;
import com.acme.exceptions.OverdraftLimitExceededException;


public class CheckingAccount extends AbstractAccount
{

	private double overdraft;

	public CheckingAccount(final int id, final double amount,
		final double overdraft)
	{
		super(id, amount);
		this.overdraft = overdraft;
	}

	@Override
	public void withdraw(final double amount) throws NotEnoughFundsException
	{
		if (amount < 0)
		{
			throw new IllegalArgumentException("Amount can not be negative");
		}
		if (this.balance < amount)
		{
			double overdraftNeeded = amount - this.balance;
			if (overdraft < overdraftNeeded)
			{
				throw new OverdraftLimitExceededException(overdraft);
			}
			else
			{
				this.balance = 0.0;
				overdraft -= overdraftNeeded;
			}
		}
		else
		{
			this.balance = this.balance - amount;
		}
	}

	@Override
	public void deposit(final double amount)
	{
		if (amount > 0)
		{
			this.balance += amount;
		}
		else
		{
			throw new IllegalArgumentException("Amount can not be negative");
		}
	}


	public AccountType getAccountType()
	{
		return AccountType.CHECKING;
	}

	public double getOverdraft()
	{
		return overdraft;
	}
}
